/***************************************************************************
 *   Copyright (C) 2008 by BogDan Vatra                                    *
 *   bogdan@licentia.eu                                                    *
 *                                                                         *
 *   This program is free software: you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation, either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 ***************************************************************************/

#include "foaie.h"
#include "database.h"
#include <QMessageBox>


using namespace SQL;

Foaie::Foaie(QWidget* parent, Qt::WFlags fl)
		: KontAMabilWindow(parent, fl), Ui::foaieForm()
{
	setupUi(this);
	connect(dataPlecare, SIGNAL(returnPressed()), SLOT(dataPlecareReturnPressed()));
	connect(navigator, SIGNAL(adauga()), SLOT(foaieInsert()));
	connect(navigator, SIGNAL(adauga()), SLOT(setReadOnly()));
	connect(navigator, SIGNAL(modifica()), SLOT(setReadOnly()));

	connect(navigator, SIGNAL(aplica()), SLOT(refreshUltimaFoaie()));
	connect(masiniLookup,SIGNAL(returnPressed()),this,SLOT(showFoi()));
	connect(tableview,SIGNAL(escapePressed()),this,SLOT(hideFoi()));
	connect(this,SIGNAL(aboutToShow()), SLOT(hideFoi()));
	connect(numarFoaie,SIGNAL(returnPressed()),this,SLOT(numarFoaieReturn()));
}

void Foaie::dataPlecareReturnPressed() 
{
	if (defaultDatabase()->query("foaie")->state()==Query::Insert)
		dataSosire->setDateTime(dataPlecare->dateTime());
}
void Foaie::foaieInsert() 
{
	defaultDatabase()->query("masini")->locate("id",id_masina);
	//defaultDatabase()->query("masini")->locate("id",defaultDatabase()->query("ultima_foaie")->value("id_masina"),false,false,false);
	defaultDatabase()->query("puncteLucru")->locate("id",defaultDatabase()->query("ultima_foaie")->value("id_punct_lucru"),false,false,false);		
	defaultDatabase()->query("foaie")->setValue( "consum_normat",defaultDatabase()->query("masini")->value("consum_normat").toDouble());         
	consumEdit->setText(defaultDatabase()->query("masini")->value("consum_normat").toString());
		
}
void Foaie::refreshUltimaFoaie() 
{	
	defaultDatabase()->query("ultima_foaie")->select();
}

bool Foaie::addFoaie(const QString & addStr, QString * result, qint64 * id)
{
	if (!defaultDatabase()->query("foaie")->canInsert())
		return false;

	defaultDatabase()->query("foaie")->insert();
	numarEdit->setText(addStr);

	numarEdit->setCursorPosition(addStr.length());

	connect(defaultDatabase()->query("foaie"),SIGNAL(afterPost()),SLOT(accept()));
	connect(defaultDatabase()->query("foaie"),SIGNAL(afterCancel()),SLOT(reject()));
	bool res=exec();
	disconnect(defaultDatabase()->query("foaie"),SIGNAL(afterPost()),this, SLOT(accept()));
	disconnect(defaultDatabase()->query("foaie"),SIGNAL(afterCancel()),this, SLOT(reject()));
	if (res)
	{
		if (result)
			*result=defaultDatabase()->query("foaie")->value("nr").toString();
		if (id)
			*id=defaultDatabase()->query("foaie")->value("id").toInt();
	}
	return res;
}
void Foaie::showFoi()
{
	defaultDatabase()->query("foaie")->bindValue( "id_masina", defaultDatabase()->query("masini")->value( "id" ) );
	defaultDatabase()->query("foaie")->bindValue( "numar","" );
	defaultDatabase()->query("foaie")->select();	
	foaieFrame->setVisible( true );
	tableview->setFocus();
	frameMasini->setEnabled(false);	
	id_masina=defaultDatabase()->query("masini")->value( "id" ).toInt();
	
}
void Foaie::hideFoi()
{
	foaieFrame->setVisible( false );
	frameMasini->setEnabled(true);
	masiniLookup->setFocus();
	numarFoaie->setText("");
	frameCautare->setVisible(false);
	layout()->activate();
	resize(QSize(width(), height() - foaieFrame->height()));
}

void Foaie::numarFoaieReturn()
{
	defaultDatabase()->query("foaie")->bindValue( "id_masina", defaultDatabase()->query("masini")->value( "id" ) );
	defaultDatabase()->query("foaie")->bindValue( "numar", numarFoaie->text().trimmed() );
	defaultDatabase()->query("foaie")->select();
	if(!defaultDatabase()->query("foaie")->locate("nr",numarFoaie->text().trimmed()))
	{
		QMessageBox msgBox;
		msgBox.setText("Nu exista foaie cu numarul " + numarFoaie->text().trimmed()+"!");
		msgBox.exec();	
		return;
	}
	foaieFrame->setVisible( true );
	tableview->setFocus();
	frameMasini->setEnabled(false);
}
void Foaie::setReadOnly()
{
	consumEdit->setDisabled(true);
}
Foaie::~Foaie()
{
}



